/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.datatype;


/**
 * A Packable class that encapsulates a float object.
 */
public class PackableFloat implements Packable {
  private static long SIZE = Float.SIZE / 8;
  private float value;
  /**
   * Constructs a newly allocated PackableFloat object that holding a
   * zero value of type float.
   */
  public PackableFloat() { this(0.0f); }
  /**
   * Constructs a newly allocated PackableFloat object that represents
   * the primitive float argument.
   * @param value the value to be represented by the PackableFloat.
   */
  public PackableFloat(float value) { this.value = value; }
  /**
   * Return the float value of this PackableFloat object.
   * @return the float value represented by this object
   */
  public float get() { return value; }
  /**
   * Indicates whether some other object is "equal to" this one.
   * see {@link Packable#equals}
   */
  @Override
  public boolean equals(Object o) {
    if (o instanceof PackableFloat) {
      if (o == this) {
        return true;
      }
      return value == ((PackableFloat) o).get();
    }
    else {
      return false;
    }
  }
  /**
   * Returns a hash code value for the object.
   * see {@link Packable#hashCode}
   */
  @Override
  public int hashCode() {
    return (new Float(value)).hashCode();
  }
  /**
   * Indicates which slot this object should be put.
   * see {@link Packable#slot}
   */
  @Override
  public int slot() {
    return this.hashCode();
  }

  @Override
  public long getRoughSize() {
    return SIZE;
  }
  
  @Override
  public String toString() {
    return "" + get();
  }
}
